package com.yfbao.horizon.schedule.tk.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JsonUtils {

    private static final ObjectMapper MAPPER = new ObjectMapper()
            .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
            .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .setSerializationInclusion(JsonInclude.Include.NON_NULL);

    public static String toJson(Object object){
        if (object == null) {
            return null;
        } else {
            try {
                return MAPPER.writeValueAsString(object);
            } catch (Exception e) {
                log.warn("write to json string error:" + object, e);
                return null;
            }
        }
    }
}
